<template>
  <div class="show">
    <div class="">
      <el-table
        :loading="listLoading"
        :data="list.slice((this.currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          align="center"
          prop="userId"
          label="用户编号"
          width="150px"
        />

        <el-table-column
          align="center"
          prop="userPwd"
          label="用户密码"
          width="200px"
        />

        <el-table-column
          align="center"
          prop="userType"
          label="用户类型"
          width="200px"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.userType=='0'">用户</span>
            <span v-if="scope.row.userType=='1'">管理员</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="userStatus"
          label="用户状态"
          width="200px"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.userStatus=='0'">已封禁</span>
            <span v-if="scope.row.userStatus=='1'">未封禁</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="修改状态" width="200px">
          <template slot-scope="Scope">
            <el-button v-if="Scope.row.userStatus=='1'" type="danger" icon="el-icon-circle-close" size="mini" @click="updateUser(Scope.row.userId,Scope.row.userPwd,0,Scope.row.userType,Scope.row.userOldStatus)">封禁</el-button>
            <el-button v-if="Scope.row.userStatus=='0'" type="success" icon="el-icon-circle-check" size="mini" @click="updateUser(Scope.row.userId,Scope.row.userPwd,1,Scope.row.userType,Scope.row.userOldStatus)">解封</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getAllUser, updateUserStatus } from '../../api/user.js'
export default {
  props: {
    type: String
  },

  data() {
    return {
      user: {
        userId: '',
        userPwd: ''
      },
      show: false,
      dialogFormVisible: false,
      search: {},
      listLoading: false,
      list: [],
      currentPage: 1, // 默认显示页面为1
      pagesize: 10, //    每页的数据条数
      formLabelWidth: '120px',
      readonly: true
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watc: {},
  created() {
    this.getAllUser()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},

  methods: {
    // 每页下拉显示数据
    handleSizeChange(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange(currentPage) {
      console.log(this)
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    // 获取所有user
    getAllUser() {
      getAllUser().then((data) => {
        console.log('获取到的所有user信息为', data)
        this.list = data.obj
        this.listLoading = false
      })
    },
    // 更新user
    updateUser(userId, userPwd, userStatus, userType, userOldStatus) {
      updateUserStatus(userId, userPwd, userStatus, userType, userOldStatus).then((data) => {
        console.log('调取接口后返回的数据为', data)
        this.getAllUser()
      })
    },
    // 展示修改密码
    showupdatePwd(userId, userPwd) {
      this.dialogTableVisible = true
      this.user.userId = userId
      this.user.userPwd = userPwd
    },
    // 取消
    closeupdatePwd() {
      this.dialogTableVisible = false
    }

  } // 如果页面有keep-alive缓存功能，这个函数会触发

}

</script>
<style>
  .show{
    width: 90%;
    display: flex;
    justify-content: center;
    padding-top: 50px;
    margin-left: 50px;
  }
</style>

